#encoding:UTF-8
import hashlib
import mylib
import MySQLdb
import datetime
import time

conn = mylib.create_conn()


def hash_url(conn):
    try:
        my_query = "CAll get_item_recent;"
        myresult = mylib.get_result_sql(conn, my_query)
        
        for i in myresult:
            id_item = i['id']
            url_item = i['url']
            url_hash = mylib.hash_url(url_item)
            
            cmd = "CALL insert_hash_url('%s','%s')"%(id_item,url_hash)
            cursor = conn.cursor(MySQLdb.cursors.DictCursor)
            cursor.execute(cmd)
            print 'insert thanh cong'
            cursor.close()
            
            
        print 'Xong tat ca'
    except:
        print 'Loi khac'
if __name__=="__main__":
    try:
        conn.ping()
    except:
        conn = mylib.create_conn()
    while True:
        print "Dang chay"
        hash_url(conn)
        time.sleep(60)